package utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.providers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.InteractionprotocolsPackage;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.Lifeline;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.expressions.UpcolbpipAbstractExpression;
import utn.frsf.cidisi.upcolbpip.interactionprotocols.diagram.part.UpcolbpipDiagramEditorPlugin;

/**
 * @generated
 */
public class ElementInitializers {

	/**
	 * @generated
	 */
	public static class Initializers {

		/**
		 * @generated
		 */
		public static final IObjectInitializer Role_2005 = new ObjectInitializer(
				InteractionprotocolsPackage.eINSTANCE.getRole()) {

			protected void init() {
				add(createNewElementFeatureInitializer(
						InteractionprotocolsPackage.eINSTANCE
								.getRole_RoleLifeline(),
						new ObjectInitializer[] { roleLifeline(), }));
			}

			ObjectInitializer roleLifeline() {
				return new ObjectInitializer(
						InteractionprotocolsPackage.eINSTANCE.getLifeline()) {

					protected void init() {
						add(createExpressionFeatureInitializer(
								InteractionprotocolsPackage.eINSTANCE
										.getLifeline_Name(),
								new UpcolbpipAbstractExpression(
										InteractionprotocolsPackage.eINSTANCE
												.getLifeline()) {

									protected Object doEvaluate(Object context,
											Map env) {
										Lifeline self = (Lifeline) context;
										return Java.initLifeline(self);
									}
								}));
					}
				};
			}
		};

		/**
		 * @generated
		 */
		private Initializers() {
		}

		/**
		 * @generated
		 */
		public static interface IObjectInitializer {

			/**
			 * @generated
			 */
			public void init(EObject instance);
		}

		/**
		 * @generated
		 */
		public static abstract class ObjectInitializer implements
				IObjectInitializer {

			/**
			 * @generated
			 */
			final EClass element;

			/**
			 * @generated
			 */
			private List featureInitializers = new ArrayList();

			/**
			 * @generated
			 */
			ObjectInitializer(EClass element) {
				this.element = element;
				init();
			}

			/**
			 * @generated
			 */
			protected abstract void init();

			/**
			 * @generated
			 */
			protected final IFeatureInitializer add(
					IFeatureInitializer initializer) {
				featureInitializers.add(initializer);
				return initializer;
			}

			/**
			 * @generated
			 */
			public void init(EObject instance) {
				for (Iterator it = featureInitializers.iterator(); it.hasNext();) {
					IFeatureInitializer nextExpr = (IFeatureInitializer) it
							.next();
					try {
						nextExpr.init(instance);
					} catch (RuntimeException e) {
						UpcolbpipDiagramEditorPlugin.getInstance().logError(
								"Feature initialization failed", e); //$NON-NLS-1$						
					}
				}
			}
		}

		/**
		 * @generated
		 */
		interface IFeatureInitializer {

			/**
			 * @generated
			 */
			void init(EObject contextInstance);
		}

		/**
		 * @generated
		 */
		static IFeatureInitializer createNewElementFeatureInitializer(
				EStructuralFeature initFeature,
				ObjectInitializer[] newObjectInitializers) {
			final EStructuralFeature feature = initFeature;
			final ObjectInitializer[] initializers = newObjectInitializers;
			return new IFeatureInitializer() {

				public void init(EObject contextInstance) {
					for (int i = 0; i < initializers.length; i++) {
						EObject newInstance = initializers[i].element
								.getEPackage().getEFactoryInstance().create(
										initializers[i].element);
						if (feature.isMany()) {
							((Collection) contextInstance.eGet(feature))
									.add(newInstance);
						} else {
							contextInstance.eSet(feature, newInstance);
						}
						initializers[i].init(newInstance);
					}
				}
			};
		}

		/**
		 * @generated
		 */
		static IFeatureInitializer createExpressionFeatureInitializer(
				EStructuralFeature initFeature,
				UpcolbpipAbstractExpression valueExpression) {
			final EStructuralFeature feature = initFeature;
			final UpcolbpipAbstractExpression expression = valueExpression;
			return new IFeatureInitializer() {

				public void init(EObject contextInstance) {
					expression.assignTo(feature, contextInstance);
				}
			};
		}

		/**
		 * @generated
		 */
		static class Java {
			/**
			 * @generated
			 */
			private static String initLifeline(Lifeline self) {
				// TODO: implement this method
				// Ensure that you remove @generated or mark it @generated NOT

				throw new UpcolbpipAbstractExpression.NoImplException(
						"No user java implementation provided in 'initLifeline' operation"); //$NON-NLS-1$
			}
		}
	}
}
